import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:memo/pages/todo_list_page.dart';
import 'package:memo/state/model/todo.dart';
import 'package:memo/state/work_record_state.dart';
import 'package:memo/state/click_state.dart';
import 'package:memo/widget/punch_widget.dart';
import 'package:provider/provider.dart';

import 'add_todo_page.dart';
import 'my_drawer_page.dart';

const waringColor = Color(0xfff1cf53);
const errorColor = Color(0xfff06165);
const successColor = Color(0xff6bd24c);

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final dateTime = DateTime.now();
    bool morningFlag = dateTime.hour >= 7 && dateTime.hour <= 9;
    bool nightFlag = dateTime.hour >= 17 && dateTime.hour <= 19;
    return Scaffold(
      appBar: AppBar(
          leading: Builder(
            builder: (context) => IconButton(
                  icon: Icon(Icons.dehaze),
                  onPressed: () {
                    Scaffold.of(context).openDrawer();
                  },
                ),
          ),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.add),
              onPressed: () => Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => AddTodoPage(null))),
            ),
          ],
          title: Consumer<WorkRecordState>(
            builder: (context, workRecordState, _) => workRecordState.isLoading
                ? Text("...")
                : (workRecordState.morningFlag == null
                    ? Text("${DateFormat("yyyy-MM-dd").format(dateTime)}")
                    : Text("待办事项")),
          )),
      drawer: const SizedBox(
        width: 240.0,
        child: Drawer(
          child: MyDrawerPage(),
        ),
      ),
      body: Center(
        child: Consumer<WorkRecordState>(
          builder: (context, workRecordState, _) {
            if (workRecordState.isLoading) {
              return const LoadingWidget();
            }

            if (morningFlag && !workRecordState.morningFlag) {
              return const PunchWidget(text: "上");
            }

            if (nightFlag && !workRecordState.nightFlag) {
              return const PunchWidget(text: "下");
            }

            return _workRecordWidget(workRecordState);
          },
        ),
      ),
    );
  }

  Widget _workRecordWidget(WorkRecordState workRecordState) {
    return workRecordState.isLoading ? const LoadingWidget() : TodoListPage();
  }

  Future<bool> _deleteDialog(BuildContext context) async {
    return showDialog<bool>(
        context: context,
        barrierDismissible: true,
        // dialog is dismissible with a tap on the barrier
        builder: (BuildContext context) {
          return AlertDialog(
            content: Text("是否删除"),
            actions: <Widget>[
              FlatButton(
                child: const Text('删除'),
                onPressed: () {
                  Navigator.of(context).pop(true);
                },
              ),
              FlatButton(
                child: const Text('返回'),
                onPressed: () {
                  Navigator.of(context).pop(false);
                },
              ),
            ],
          );
        });
  }

  Widget _itemWidget(Todo todo) {
    return Container(
      padding: EdgeInsets.only(left: 10.0, right: 10.0, top: 10.0),
      height: 160.0,
      child: Card(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            const SizedBox(height: 6.0),
            Padding(
              padding: const EdgeInsets.only(right: 10.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  IconButton(
                    icon: Icon(
                      Icons.brightness_1,
                      color: todo.type == TodoType.success
                          ? successColor
                          : (todo.type == TodoType.waring
                              ? waringColor
                              : errorColor),
                    ),
                    onPressed: () {},
                  ),
                  Text(
                    "${DateFormat("MM-dd HH:mm").format(todo.time)}",
                    style:
                        TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0),
                  )
                ],
              ),
            ),
            const SizedBox(height: 6.0),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 12.0),
              child: Text("${todo.note}",
                  maxLines: 3, overflow: TextOverflow.ellipsis),
            ),
          ],
        ),
      ),
    );
  }
}

class LoadingWidget extends StatelessWidget {
  final double width;

  const LoadingWidget({Key key, this.width = 30.0}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: SizedBox(
        width: width,
        height: width,
        child: CircularProgressIndicator(),
      ),
    );
  }
}
